﻿define(['ko', 'utils','config', 
'router', 
'viewmodels/vm', 
'presenter',
'repositories/repository.productcategories'],
function(ko,utils, config, router, vm, presenter, repoProductCategories) {
	var productCategories = ko.observableArray();
	var productCabinets = ko.observableArray();
	var cabinet = ko.observableArray();
	
	var getProductCategories = function(){
		repoProductCategories.getProductCategories({				
			results : productCategories
		}).done(function() {
			console.log("aaa"+ productCategories());
		}); 
	};
	
	function getCookie(cname) {
	    var name = cname + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0; i<ca.length; i++) {
	        var c = ca[i];
	        while (c.charAt(0)==' ') c = c.substring(1);
	        if (c.indexOf(name) != -1) 
	        	return c.substring(name.length,c.length);
	    }
	    return "";
	}
	
	var logger = config.logger, register = function() {
		//getTotalProducts();
		//getCabinet("TD900");
		getProductCategories();
		//alert("products==" + productCategories().length);
		var routeData = [
		{
			view : config.viewIds.store,
			routes : [{
				isDefault : true,
				route : config.hashes.store + "/:categoryid",
				title : config.viewTitles.store,
				callback : function(routeData, callback) {
					console.log(routeData);
					
					vm.getPageConfig("002").then(function(){
						
						var parentCategory = "";
						var childCategory = "";
						
						for (var i = 0; i < productCategories().length; i++){
							for(var j=0;j<productCategories()[i].children().length; j++){
								if(productCategories()[i].children()[j].id() === routeData.categoryid){
									parentCategory = productCategories()[i].name();
									childCategory = productCategories()[i].children()[j].name();
								}
							}
						}
						vm.vmPagePath.value.push({
					      "pageid": "002",
					      "parentpageid": "001",
					      "pagetitle": parentCategory,
					      "shorttitle": "dsfsgs",
					      "url": "#/store/1"
    					});	
    					
						vm.vmPagePath.value.push({
					      "pageid": "002",
					      "parentpageid": "001",
					      "pagetitle": childCategory,
					      "shorttitle": "dsfsgs",
					      "url": "#/store/" + routeData.categoryid
    					});	
					});
					
					vm.store.activate(routeData);
				},
				group : '.route-top-store'
			}]
		},
		{
			view : config.viewIds.store,
			routes : [{
				isDefault : true,
				route : config.hashes.store + "/:categoryid/product_detail/:id",
				title : config.viewTitles.product_detail,
				callback : function(routeData, callback) {
					console.log(routeData);
					//getCabinet(routeData.id);
					vm.getPageConfig("002").then(function(){
						
						var parentCategory = "";
						var childCategory = "";
						
						for (var i = 0; i < productCategories().length; i++){
							for(var j=0;j<productCategories()[i].children().length; j++){
								if(productCategories()[i].children()[j].id() === routeData.categoryid){
									parentCategory = productCategories()[i].name();
									childCategory = productCategories()[i].children()[j].name();
								}
							}
						}
						
						vm.vmPagePath.value.push({
							  "pageid": "002",
							  "parentpageid": "001",
							  //"pagetitle": routeData.categoryid,
							  "pagetitle": parentCategory,
							  "shorttitle": "dsfsgs",
							  "url": "#/store/" + routeData.categoryid
							});	
	
    					vm.vmPagePath.value.push({
					      "pageid": "002",
					      "parentpageid": "001",
					      "pagetitle": childCategory,
					      "shorttitle": "dsfsgs",
					      "url": "#/store/" + routeData.categoryid
    					});
    					console.log("cookie==="+document.cookie);
    					
    					vm.vmPagePath.value.push({
					      "pageid": "002",
					      "parentpageid": "001",
					      "pagetitle": getCookie("cabinetName"),
					      "shorttitle": "dsfsgs",
					      "url": "#/store/" + routeData.categoryid + "/product_detail/"+ routeData.id
    					});
					});
					
					vm.cabinet.activate(routeData);
				},
				group : '.route-top-product-detail'
			}]
		}
		, {
			view : config.viewIds.product_detail,
			routes : [{
				isDefault : false,
				route : config.hashes.product_detail+"/:id",
				title : config.viewTitles.product_detail,
				
				callback : function(routeData, callback) {
					vm.getPageConfig("002");
					vm.cabinet.activate(routeData);
				},
				group : '.route-top-product-detail'
			}]
		}, {
			view : config.viewIds.link2,
			routes : [{
				isDefault : false,
				route : config.hashes.link2,
				title : config.viewTitles.link2,
				callback : function(routeData, callback) {
					vm.vmLink2.activate();
				},
				group : '.route-top-link2'
			}]
		},
		// Invalid routes
		{
			view : '',
			route : /.*/,
			title : '',
			callback : function() {
				logger.error(config.toasts.invalidRoute);
			}
		}];

		for (var i = 0; i < routeData.length; i++) {
			router.register(routeData[i]);
		}

		router.run();
	};

	return {
		register : register
	};
}); 